
package com.yanboo.modules.system.rest;

import com.yanboo.common.annotation.CustomController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import com.yanboo.logging.annotation.Log;
import com.yanboo.common.exception.BadRequestException;
import com.yanboo.modules.system.domain.Job;
import com.yanboo.modules.system.service.JobService;
import com.yanboo.modules.system.service.dto.JobDto;
import com.yanboo.modules.system.service.dto.JobQueryCriteria;
import com.yanboo.common.utils.PageResult;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * 系统：岗位管理
 */
@Tag(name = "系统:岗位管理")
@CustomController
@RequiredArgsConstructor
@RequestMapping("/api/job")
public class JobController {

    private final JobService jobService;
    private static final String ENTITY_NAME = "job";

    /**
     * 导出岗位数据
     * @param response
     * @param criteria
     * @throws IOException
     */
    @Operation(summary = "导出岗位数据")
    @GetMapping(value = "/download")
    public void exportJob(HttpServletResponse response, JobQueryCriteria criteria) throws IOException {
        jobService.download(jobService.queryAll(criteria), response);
    }

    /**
     * 查询岗位
     * @param criteria
     * @param pageable
     * @return
     */
    @Operation(summary = "查询岗位")
    @GetMapping
    public ResponseEntity<PageResult<JobDto>> queryJob(JobQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(jobService.queryAll(criteria, pageable),HttpStatus.OK);
    }

    /**
     * 新增岗位
     * @param resources
     * @return
     */
    @Operation(summary = "新增岗位")
    @Log("新增岗位")
    @PostMapping
    public ResponseEntity<Object> createJob(@Validated @RequestBody Job resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        jobService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 修改岗位
     * @param resources
     * @return
     */
    @Operation(summary = "修改岗位")
    @Log("修改岗位")
    @PutMapping
    public ResponseEntity<Object> updateJob(@Validated(Job.Update.class) @RequestBody Job resources){
        jobService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除岗位
     * @param ids
     * @return
     */
    @Operation(summary = "删除岗位")
    @Log("删除岗位")
    @DeleteMapping
    public ResponseEntity<Object> deleteJob(@RequestBody Set<Long> ids){
        // 验证是否被用户关联
        jobService.verification(ids);
        jobService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}